﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;

namespace WebSys.UI.OfficeFlow
{
    public class WorkFlowBeforeSubmitSkin : MPBasicTemplate
    {
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        MPRepeater FlowMPRepeater;
        string wiid, ctlId, iid, wid, userid, step;
        StepType stepType;
        SendMethod sendMethod;
        protected string UserId
        {
            get { if (base.Page.Session["UserId"] != null) { return base.Page.Session["UserId"].ToString(); } else { return string.Empty; } }
            set { base.Page.Session["UserId"] = value; }
        }
        protected override void OnLoad(EventArgs e)
        {
            LoginHelper.CheckLogin();
            InitControl();
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                Initcy();
            }
        }

        private void InitControl()
        {
            wiid = base.Page.Request["wiid"];
            ctlId = HttpContext.Current.Server.UrlDecode(base.Page.Request["CTLID"]);
            iid = base.Page.Request["iid"];
            wid = _FlowFactoryProvider.GetWidByIID(iid);
            userid = UserId;
            step = _FlowFactoryProvider.GetStepName(ctlId,wid);
            sendMethod = _FlowFactoryProvider.GetSendMethod(wid, ctlId);
            stepType = _FlowFactoryProvider.GetStepTypeByStep(wid, ctlId);
            bool canopen = _FlowFactoryProvider.CanOpen(userid, iid, wiid, step);
            bool isopen = _FlowFactoryProvider.IsOpen(iid, wiid, step);
            //if (!canopen || isopen)
            //{
            //    Antu.Utilities.UtilitiesPage.AlertDialogClear("该业务已被处理或无权限处理！", "window.parent.closeDialog();");
            //    return;
            //}
            string msg = "";
            if (false == _FlowFactoryProvider.TaskOpenCheck(iid, wiid, step, userid, out msg))
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("alert('" + msg + "！');", "window.parent.closeDialog();");
                return;
            }
            FlowMPRepeater = (MPRepeater)FindControl("FlowMPRepeater");
            FlowMPRepeater.ItemDataBound += FlowMPRepeater_ItemDataBound;
            DataTable dt = _FlowFactoryProvider.GetStepAllNextFlowNames(long.Parse(iid), long.Parse(wiid), ctlId);
            if (dt != null && dt.Rows.Count > 0)
            {
                FlowMPRepeater.DataSource = dt;
                FlowMPRepeater.DataBind();
            }
        }

        private void FlowMPRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            MPRepeater RoleMPRepeater = (MPRepeater)e.Item.FindControl("RoleMPRepeater");
            PlaceHolder flowradioPanel = (PlaceHolder)e.Item.FindControl("flowradioPanel");
            RoleMPRepeater.ItemDataBound += RoleMPRepeater_ItemDataBound;
            DataRowView drv = (DataRowView)e.Item.DataItem;
            DataTable dt = _FlowFactoryProvider.GetRolesInStep(wid, drv["fname"].ToString(), iid, userid);
            if (dt != null && dt.Rows.Count > 0)
            {
                RoleMPRepeater.ExtAttr = drv["fname"].ToString();
                RoleMPRepeater.DataSource = dt;
                RoleMPRepeater.DataBind();
            }

            Literal rb = new Literal();
            rb.Text = "<input type='radio' id='flows' name='flows' onclick='checkflowuser(this);' value='" + drv["fname"].ToString() + "' />";
            if (stepType == StepType.MutiFlowStart)
            {
                rb.Text = "<input type='checkbox' id='flows' name='flows' onclick='checkflowuser(this);'  value='" + drv["fname"].ToString() + "' />";
            }
            flowradioPanel.Controls.Add(rb);
        }

        private void RoleMPRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            string flowname = ((MPRepeater)sender).ExtAttr;
            PlaceHolder roleckPanel = (PlaceHolder)e.Item.FindControl("roleckPanel");
            MPRepeater UserMPRepeater = (MPRepeater)e.Item.FindControl("UserMPRepeater");
            UserMPRepeater.ItemDataBound += UserMPRepeater_ItemDataBound;
            DataRowView drv = (DataRowView)e.Item.DataItem;
            DataTable dt = _FlowFactoryProvider.GetRoleUserInStep(long.Parse(iid), long.Parse(wiid), drv["group_name"].ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                UserMPRepeater.ExtAttr = flowname;
                UserMPRepeater.DataSource = dt;
                UserMPRepeater.DataBind();
            }
            Literal rb = new Literal();
            if (sendMethod == SendMethod.MultiSend || sendMethod == SendMethod.AllSend)//全部或多选提交
                rb.Text = "<input type='checkbox' name='Role_" + flowname + "' onclick='checkroleuser(this);'  />" + drv["group_name"].ToString();
            else//单选提交
                rb.Text = "<input type='radio' name='Role_" + flowname + "'  onclick='checkroleuser(this);' />" + drv["group_name"].ToString();

            roleckPanel.Controls.Add(rb);
        }

        private void UserMPRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            string flowname = ((MPRepeater)sender).ExtAttr;
            PlaceHolder userckPanel = (PlaceHolder)e.Item.FindControl("userckPanel");
            DataRowView drv = (DataRowView)e.Item.DataItem;
            Literal rb = new Literal();
            string usertext = "User_" + flowname;
            string usertextnum = "User_" + flowname + e.Item.ItemIndex;
            if (sendMethod == SendMethod.MultiSend || sendMethod == SendMethod.AllSend)//全部或多选提交
                rb.Text = "<input id='" + usertextnum + "' type='checkbox' name='" + usertext + "' value='" + drv["userid"] + "' />" + drv["user_name"];
            else//单选提交
                rb.Text = "<input id='" + usertextnum + "' type='radio' name='" + usertext + "' value='" + drv["userid"] + "' />" + drv["user_name"];
            userckPanel.Controls.Add(rb);
        }

        private void Initcy()
        {

        }


    }
}


